// pages/retail/goods/goods.js
import api from '../../../utils/api'
import message from '../../../utils/message';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        retail_product_list:[],
        has_load:false,
        tab_index:0,
        retail_id:null,
        user_retail_product_list:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        console.log( options );

        if(options.id){

            //this.getRetailProductList( );

            this.setData({
                retail_id: options.id
            })

            this.getUserRetailProductList();
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getRetailProductList:function (  ) {
        var me = this;
        message.show_loading();
        api.retail.getRetailProductList({} , function (response) {
            message.hide_loading();
            if(response.status==1){
                me.setData({
                    retail_product_list: response.data,
                    has_load:true
                })
            }else{
                message.show_error( response.message );
            }
        })
    },
    removeUserRetailProduct:function( e ){
        //从我的店铺移除代理商品
        var me = this;

        var proid = e.currentTarget.dataset.proid;

        var params = {};
        params.product_id = proid;
        params.type=1;
        api.retail.removeUserRetailProduct( params , function (response) {
            console.log(response)
            if( response.status==1 ){
                message.show_error( response.message , function () {
                    me.getRetailProductList();
                    
                });
            }else{
                message.show_error( response.message );
            }
        })


    },
    addUserRetailProduct:function (e) {
        //添加商品代理
        var me = this;
        var proid = e.currentTarget.dataset.proid;

        var params = {};
        params.product_id = proid;
        params.type=1;

        message.show_loading("操作执行中");
        api.retail.addUserRetailProduct( params , function (response) {
            message.hide_loading();
            console.log(response)
            if( response.status==1 ){
                message.show_error( response.message , function () {
                    me.getRetailProductList();
                } );
            }else{
                message.show_error( response.message );
            }
        })

    },

    updateUserRetailProduct:function (e) {
        var me = this;
        var proid = e.currentTarget.dataset.proid;

        var params = {};
        params.product_id = proid;
        params.type=2;

        api.retail.removeUserRetailProduct( params , function (response) {
            console.log(response)
            if( response.status==1 ){
                message.show_error( response.message , function () {
                    me.getRetailProductList();
                } );
            }else{
                message.show_error( response.message );
            }
        })
        
    },
    showProductQrcode:function (e) {
        var proid = e.currentTarget.dataset.proid;
    },
    getUserRetailProductList:function(){
        //加载用户分销商品列表
        var me = this;

        var params = {}
        params.retail_id = this.data.retail_id;

        api.retail.getUserRetailProductList(  params , function( response ){
            if(response.status==1){

                console.log( response.data )
                me.setData({
                    user_retail_product_list: response.data
                })
            }else{
                message.show_error( response.message );
            }
        })
    }
    
})